<?php

namespace app\admin\controller;

use app\model\Points;
use app\model\StoreCate;
use think\exception\ValidateException;
use think\facade\Validate;
use app\model\Technical;
use app\model\Agent;
use app\model\Partner;
use app\model\Tuanzhang;
use app\model\Store;
use app\model\Operatingcity;
use app\model\RegisterField;
use app\model\Category;
use app\model\TechnicalCertificate;
use app\model\Users;
use app\model\Member;
use app\model\Openid;

class DiyfieldsController extends Base
{
	function getModel()
	{
		$ptype = $this->getPath()[1];
		if ($ptype == 'technical') {
			return new Technical;
		} else if ($ptype == 'agent') {
			return new Agent;
		} else if ($ptype == 'member') {
			return new Member;
		} else if ($ptype == 'operatingcity') {
			return new Operatingcity;
		} else if ($ptype == 'store') {
			return new Store;
		} else if ($ptype == 'partner') {
			return new Partner;
		} else if ($ptype == 'tuanzhang') {
			return new Tuanzhang;
		}
	}
	function getPath()
	{
		$path = input('post.path', '', 'serach_in');
		if (!empty($path)) {
			$tmppath = explode('/', $path);
		}
		if (empty($tmppath)) {
			$con = input('con');
			if (!empty($con)) {
				$tmppath[1] = $con;
			}
		}

		return $tmppath;
	}

	public function update()
	{
		$id = $this->request->post('id');
		$infodata = input('post.infodata');
		$postdata = only('id,fields');

		$regdata = RegisterField::fieldToData($postdata, 'pc');

		$data = $regdata['data'];
		unset($data['create_time']);

		//验证数据开始
		$checkdata = $data;
		if (empty($checkdata['password'])) {
			$checkdata['password'] = '12345678';
		}

		$validatemod = Validate::rule($regdata['rule']);
		if (!$validatemod->check($checkdata)) {
			throw new ValidateException($validatemod->getError());
		}
		//验证数据结束

		if (!empty($data['username'])) {
			$data['username'] = trim($data['username']);
		}

		if (empty($data['password']) && empty($id)) {
			$data['password'] = '123456';
		}
		if (!empty($data['password'])) {
			$data["salt"] = substr(md5(uniqid()), 8, 8);
			$data['password'] = pass_hash($data['password'], $data["salt"]);
		}

		if ($this->getPath()[1] == 'member') {
			if ($data['username']) {
				$checknamewhere['username'] = $data['username'];

				if (empty($id)) {
					if (Member::where($checknamewhere)->find()) {
						throw new ValidateException('用户名已被占用');
					}
				} else {
					if (Member::where($checknamewhere)->where('id', '<>', $id)->find()) {
						throw new ValidateException('用户名已被占用');
					}
				}
			}

			if (empty($data['password'])) {
				unset($data['password']);
			}
		} else {
			if (empty($id)) {
				$usersdata['username'] = $data['username'];
			}

			if ($usersdata['username']) {
				if (Users::where('username', $usersdata['username'])->find()) {
					throw new ValidateException('用户名已被占用');
				}
			}

			$usersdata['password'] = $data['password'];
			$usersdata['salt'] = $data['salt'];

			unset($data['salt']);
			unset($data['password']);
			unset($data['username']);
		}

		/*
		if ($this->getPath()[1] == 'technical') {
			if ($data['photoalbum'] && is_string($data['photoalbum'])) {
				$data['touxiang'] = explode(',', $data['photoalbum'])[0];
			}
		}
		*/

		if (empty($id)) {
			$data['uid'] = 0;
			$data['weid'] = weid();
			$data['sort'] = 100;
			try {
				$res = $this->getModel()->create($data);
				if ($this->getPath()[1] == 'store' || $this->getPath()[1] == 'technical' || $this->getPath()[1] == 'tuanzhang' || $this->getPath()[1] == 'operatingcity') {

					if (empty($usersdata['username'])) {
						$usersdata['username'] = $this->getPath()[1] . $res->id;
					}

					if ($res->id && !empty($usersdata['username'])) {
						if (empty($usersdata['password'])) {
							unset($usersdata['password']);
						}
						if ($this->getPath()[1] == 'store') {
							$usersdata['sid'] = $res->id;
						}
						if ($this->getPath()[1] == 'operatingcity') {
							$usersdata['ocid'] = $res->id;
						}
						if ($this->getPath()[1] == 'tuanzhang') {
							$usersdata['tzid'] = $res->id;
						}

						if ($this->getPath()[1] == 'technical') {
							$usersdata['tid'] = $res->id;
						}

						$usersdata['mobile'] = $data['tel'];
						$usersdata['weid'] = weid();
						$usersdata['w7uid'] = 0;
						$Usersres = Users::create($usersdata);

						$this->getModel()->where('id', $res->id)->update(['uid' => $Usersres->id]);
					}
				}
			} catch (\Exception $e) {
				throw new ValidateException($e->getMessage());
			}
			return $this->json(['msg' => '添加成功', 'data' => $res->id]);
		} else {
			$data['id'] = $id;

			if ($this->getPath()[1] == 'technical') {
				$Technicalwhere['id'] = $data['id'];
				$Technicalwhere['province_name'] = $data['province_name'];
				$Technicalwhere['city_name'] = $data['city_name'];
				$Technicalwhere['district_name'] = $data['district_name'];
				$Technicalwhere['house_number'] = $data['house_number'];

				$Technicalcheck = Technical::where($Technicalwhere)->find();
				if ($Technicalcheck) {
					if (!empty($Technicalcheck['latitude']) && !empty($Technicalcheck['longitude'])) {
						unset($data['latitude']);
						unset($data['longitude']);
					}
					unset($data['region_name']);
				}
			}
			if ($this->getPath()[1] == 'store') {
				$storewhere['id'] = $data['id'];
				$storewhere['province_name'] = $data['province_name'];
				$storewhere['city_name'] = $data['city_name'];
				$storewhere['district_name'] = $data['district_name'];
				$storewhere['house_number'] = $data['house_number'];
//                var_dump($data);
				$Storecheck = Store::where($storewhere)->find();
//                var_dump($Storecheck);
				if ($Storecheck) {
					if (!empty($Storecheck['latitude']) && !empty($Storecheck['longitude'])) {
						unset($data['latitude']);
						unset($data['longitude']);
					}
					unset($data['region_name']);
				}
			}



			try {
				if ($this->getPath()[1] == 'technical') {
					if (!empty($usersdata['password'])) {
						$updateres = Users::where('tid', $data['id'])->update($usersdata);
						if (!$updateres && $infodata['id']) {
							Users::where('id', $infodata['id'])->update($usersdata);
						}
					} else {
						unset($usersdata['password']);
					}
				}

                if($this->getPath()[1] =='member'){
                    $memberInfo = Member::where('id', $infodata['id'])->find();
                    if($memberInfo['points'] < $data['points']){
                        // 增加积分
                        $points = $data['points'] - $memberInfo['points'];
                        Points::create([
                            'weid' => weid(),
                            'uid' => $infodata['id'],
                            'points' => $points,
                            'description' => '后台积分调整',
                            'prefix' => 1,
                            'create_time' => time(),
                            'type' => 2,
                            'from_type' => 1,
                            'admin_id' => $this->userInfo['id'] ?? UID(),
                        ]);
                    }elseif ($memberInfo['points'] > $data['points']){
                        // 减少积分
                        $points = $memberInfo['points'] - $data['points'];
                        Points::create([
                            'weid' => weid(),
                            'uid' => $infodata['id'],
                            'points' => $points,
                            'description' => '后台积分调整',
                            'prefix' => 2 ,
                            'create_time' => time(),
                            'type' => 2,
                            'from_type' => 1,
                            'admin_id' => $this->userInfo['id'] ?? UID(),
                        ]);
                    }
                }

				$this->getModel()->update($data);
			} catch (\Exception $e) {
				throw new ValidateException($e->getMessage());
			}
			return $this->json(['msg' => '操作成功']);
		}
	}
	public function audit()
	{
		$id = $this->request->post('id');
		$data['id'] = $id;
		unset($data['create_time']);
		$data['status'] = 1;

		try {
			$this->getModel()->update($data);
		} catch (\Exception $e) {
			throw new ValidateException($e->getMessage());
		}
		return $this->json(['msg' => '审核成功']);
	}

	function getInfo()
	{
		$id =  $this->request->post('id', '', 'serach_in');
		$path = input('post.path', '', 'serach_in');
		$is_tzstore =  $this->request->post('is_tzstore', '', 'serach_in');
		if ($is_tzstore == 1) {
			$id = Store::getidbytzid($id);
		}

		$ptype = $this->getPath()[1];
		$data = RegisterField::getinputField($ptype);

		if (!empty($id)) {

			$infodata = $this->getModel()->find($id);

			if (!empty($infodata)) {
				$infodata = $infodata->toArray();
				$infodata['password'] = "";

				if (!empty($infodata['uid'])) {

					$infodata['user'] = Users::getuserbyid($infodata['uid']);
					unset($infodata['user']['password']);
					unset($infodata['user']['salt']);
					$infodata['username'] = $infodata['user']['username'];

					if (!empty($infodata['user']['id'])) {
						$infodata['Openid'] = Openid::getMpOpenidbyuid($infodata['user']['id']);
					}
				}

				$area[0] = areaconversion($infodata['province_name']);
				$area[1] = $infodata['city_name'];
				if (!empty($infodata['district_name'])) {
					$area[2] = $infodata['district_name'];
				}

				$infodata['region_name'] = $area;

				$customtext = iunserializer($infodata['customtext']);
			}
		}

		foreach ($data as &$vo) {

			if ($vo['is_sys'] == 1) {
				if (!empty($infodata[$vo['fieldsmingcheng']])) {
					$vo['fieldsvalue'] = $infodata[$vo['fieldsmingcheng']];
				}
				if ($vo['fieldsmingcheng'] == "sex") {
					$vo['selectvaluearray'] = sexarray();
				}

				if ($vo['fieldsmingcheng'] == 'sid') {
					$vo['selectvaluearray'] = Store::getpcarray();
				}

				if ($vo['fieldsmingcheng'] == 'level') {
					if ($vo['ptype'] == 'technical') {
						$vo['selectvaluearray'] = \app\model\TechnicalLevel::getpcarray();
					} elseif ($vo['ptype'] == 'tuanzhang') {
						$vo['selectvaluearray'] = \app\model\TuanzhangLevel::getpcarray();
					} elseif ($vo['ptype'] == 'partner') {
						$vo['selectvaluearray'] = \app\model\PartnerLevel::getpcarray();
					} elseif ($vo['ptype'] == 'operatingcity') {
						$vo['selectvaluearray'] = \app\model\OperatingcityLevel::getpcarray();
					} elseif ($vo['ptype'] == 'store') {
						$vo['selectvaluearray'] = \app\model\StoreLevel::getpcarray();
					}
				}
				if ($vo['fieldsmingcheng'] == 'agent_level') {
					if ($vo['ptype'] == 'agent') {
						$vo['selectvaluearray'] = \app\model\AgentLevel::getpcarray();
					}
				}

				if ($vo['fieldsmingcheng'] == 'areatype') {
					$vo['selectvaluearray'] = \app\model\OperatingcityType::getpcarray();
				}

				if ($vo['fieldsmingcheng'] == 'gid') {
					$vo['selectvaluearray'] = \app\model\MemberAuthGroup::getpcarray();
				}

                if($vo['fieldsmingcheng'] == 'cate'){
                    $vo['selectvaluearray'] = StoreCate::getpcarray();
                }

				if ($vo['inputtype'] == 'checkbox' && $vo['fieldsmingcheng'] == 'cate_ids') {
					if (empty($vo['fieldsvalue'])) {
						$vo['fieldsvalue'] = [];
					} else {
						$vo['fieldsvalue'] =  explode(',', $vo['fieldsvalue']);
					}
					if ($vo['ptype'] == 'technical') {
						$ctopptype = 2;
					} else {
						$ctopptype = '';
					}
					$vo['selectvaluearray'] = Category::gettoparray($ctopptype);
				}
				if ($vo['inputtype'] == 'checkbox' && $vo['fieldsmingcheng'] == 'certificate_ids') {
					if (empty($vo['fieldsvalue'])) {
						$vo['fieldsvalue'] = [];
					} else {
						$vo['fieldsvalue'] =  explode(',', $vo['fieldsvalue']);
					}
					$vo['selectvaluearray'] = TechnicalCertificate::getpcarray($ctopptype);
				}
			} else {
				$vo['fieldsvalue'] = $customtext[$vo['inputtype']][$vo['id']];
				if (!empty($vo['selectvalue'])) {
					$selectvaluearray = explode(',', $vo['selectvalue']);
					if (is_array($selectvaluearray)) {
						foreach ($selectvaluearray as $key => $svo) {
							$vo['selectvaluearray'][$key]['val'] = $svo;
							$vo['selectvaluearray'][$key]['key'] = $svo;
						}
					}
				}
			}

			if ($vo['inputtype'] == 'pics') {
				$vo['fieldsvalue1'] = $vo['fieldsvalue'];
				if (empty($vo['fieldsvalue'])) {
					$vo['fieldsvalue'] = [];
				} else {
					$vo['fieldsvalue'] = setPicsView($vo['fieldsvalue']);
				}
			}

			if ($vo['inputtype'] == 'date') {
				$vo['fieldsvalue'] =  time_format($vo['fieldsvalue']);
			}
		}
		if (!empty($infodata)) {
			$retdata['id'] = $infodata['id'];
			$retdata['infodata'] = $infodata;
		}

		$retdata['fields'] = $data;
		$retdata['path'] = $path;

		return $this->json(['data' => $retdata]);
	}

	function delete()
	{
		return $this->del($this->getModel());
	}
	/*
 	*  导入
 	*/
	public function importData()
	{
		$data = input('post.');
		$ptype = $this->getPath()[1];
		$mob = $this->getModel();
		RegisterField::importData($mob, $ptype, $data);
		return $this->json(['msg' => '操作成功']);
	}

	public function get_idbytitle($list, $title)
	{
		foreach ($list as  $v) {
			if ($v['key'] == $title) {
				return $v['val'];
			}
		}
		return 0;
	}
	public function get_idbyarea_name($list, $area_name)
	{
		foreach ($list as  $v) {
			if ($v['area_name'] == $area_name) {
				return $v['area_id'];
			}
		}
		return 0;
	}
}
